<template>
	<view>
		<search ref="search" @search="handleSearch" />
		<view v-show="pageShow">
			<view class="list-card" v-if="!!list.length">
				<uni-card v-for="(item, index) in list" :key="index" :title="'入库单号：' + item.billNo" @click="upper(item.id, item.inBillState)">
					<view class="item">
						<view class="item-content">
							<label>状态：</label>
							<text class="status" :class="{warning: item.inBillState < '600'}">{{item.inBillStateName || '-'}}</text>
						</view>
						<view class="item-content">
							<label>类型：</label>
							<text>{{item.inOrderTypeName || '-'}}</text>
						</view>
					</view>
					<view class="item">
						<view class="item-content">
							<label>外部单号：</label>
							<text>{{item.outOrderNo || '-'}}</text>
						</view>
						<view class="item-content">
							<label>预期收货：</label>
							<text>{{item.expectedArrivalTime ? $formatter.formatDate(new Date(item.expectedArrivalTime)) : '-'}}</text>
						</view>
					</view>
					<view class="item">
						<view>
							<label>供应商：</label>
							<text>{{item.supplierName || '-'}}</text>
						</view>
					</view>
					<view class="item-footer">
						<view class="footer-item">
							<label>品项数量</label>
							<text>{{item.breedCount ? parseInt(item.breedCount) : '-'}}</text>
						</view>
						<view class="footer-item">
							<label>已收数量</label>
							<text>{{item.totalGoods ? parseInt(item.totalGoods) : '-'}}</text>
						</view>
						<view class="footer-item">
							<label>上架数量</label>
							<text>{{item.totalgrounding ? parseInt(item.totalgrounding) : '-'}}</text>
						</view>
					</view>
				</uni-card>
				<uni-load-more :status="loadMoreStatus"></uni-load-more>
			</view>
			<no-data :visible="!list.length" title="暂无数据" :fullscreen="true"></no-data>
		</view>
	</view>
</template>

<script>
	import search from './search.vue';
	import uniCard from '@/components/uni-card/uni-card.vue';
	import loadMore from '@/components/uni-load-more/uni-load-more.vue';
	import noData from '@/components/no-data/no-data.vue';

	export default {
		components: {
			search,
			uniCard,
			loadMore,
			noData
		},
		data() {
			return {
				pageShow: false,
				params: {
					limit: global.$limit,
					page: 1,
					warehouseId: this.$storage.getWarehouseId()
				},
				list: [],
				loadMoreStatus: global.$loadMoreStatus.more
			}
		},
		onShow() {
			if (global.$refresh) {
				global.$refresh = false;
				this.load();
			}
		},
		onLoad() {
			this.load();
		},
		onNavigationBarButtonTap() {
			this.$refs.search.open(Object.assign({}, this.params));
		},
		onPullDownRefresh() {
			this.params.page = 1;
			this.fetch();
		},
		onReachBottom() {
			if (this.loadMoreStatus === global.$loadMoreStatus.more) {
				this.loadMoreStatus = global.$loadMoreStatus.loading;
				this.params.page++;
				this.fetch();
			}
		},
		methods: {
			load() {
				uni.showLoading({
					title: '加载中'
				});
				this.fetch();
			},
			handleSearch(param) {
				this.params = Object.assign({}, {
					limit: global.$limit,
					page: 1,
					warehouseId: this.$storage.getWarehouseId()
				}, param);
				this.load();
			},
			fetch() {
				this.$api.upperCargo.list(this.params).then(res => {
					this.params.page === 1 && uni.pageScrollTo({
						scrollTop: 0,
						duration: 300
					});
					this.list = this.params.page === 1 ? [...res.data] : [...this.list, ...res.data];
					this.loadMoreStatus = this.$pageHelper.lastPage(this.params.limit, this.params.page, res.count) ?
						global.$loadMoreStatus.noMore : global.$loadMoreStatus.more;
					this.pageShow = true;
					uni.hideLoading();
					uni.stopPullDownRefresh();
				}, () => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
				});
			},
			upper(id, status) {
				if (status < '600') {
					uni.navigateTo({
						url: '/pages/inWarehouse/upper/upperPallet?id=' + id
					});
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.list-card /deep/ .uni-card {
		margin: 0 0 30rpx 0;
	}

	.list-card /deep/ .uni-card .uni-card__header {
		padding: 10rpx 24rpx;
	}

	.uni-load-more {
		margin-top: -30rpx;
	}
</style>
